// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Label)
    total: cc.Label = null;

    @property(cc.Node)
    contentView: cc.Node = null;

    // 四个小鸟组成一组动画
    @property(cc.Sprite)
    bird0: cc.Sprite = null;

    @property(cc.Sprite)
    bird1: cc.Sprite = null;

    @property(cc.Sprite)
    bird2: cc.Sprite = null;

    @property(cc.Sprite)
    bird3: cc.Sprite = null;

    @property(cc.Node)
    birdParent: cc.Node = null;

    // 背景图
    @property(cc.Node)
    bg0: cc.Node = null;

    @property(cc.Node)
    bg1: cc.Node = null;

    // 管子
    @property(cc.Node)
    pipePraent0: cc.Node = null;

    @property(cc.Node)
    pipePraent1: cc.Node = null;

    @property(cc.Node)
    pipePraent2: cc.Node = null;

    // 开始界面
    @property(cc.Node)
    readyGOView: cc.Node = null;

    // 结束界面
    @property(cc.Node)
    gameOverView: cc.Node = null;

    // 小鸟 形态切换 时间
    time: number = 0;
    // 小鸟 下落加速度
    speed: number = 0;
    // 分数
    score: number = 0;
    // 是否游戏结束 gameOver
    isOver: boolean = false;
    // 是否是第一次 进入游戏
    isFirstPlay: boolean = true;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        cc.game.setFrameRate(45);
    }

    start() {
        
        this.initPipe();
    }

    // 按照一定时间切换要显示的小鸟精灵 切换后清零这个时间
    update(dt: number) {
        if(this.isOver){return;}

        if(this.isFirstPlay){
            this.readyGOView.active = true;
            return;
        }

        // 小鸟动起来
        this.bridyMovement(dt);

        // 背景向←移动
        this.moveBG(this.bg0);
        this.moveBG(this.bg1);

        // 移动罐子
        this.movePipe(this.pipePraent0);
        this.movePipe(this.pipePraent1);
        this.movePipe(this.pipePraent2);

        // 检测是否发生碰撞
        this.checkCollision(this.birdParent, this.pipePraent0)
        this.checkCollision(this.birdParent, this.pipePraent1)
        this.checkCollision(this.birdParent, this.pipePraent2)
    }


    // 开始
    resetGame() {
        this.isFirstPlay = false;
        this.isOver = false;
        this.readyGOView.active = false;
        this.gameOverView.active = false;

        this.initPipe();
        // 小鸟 形态切换 时间
        this.time = 0;
        // 小鸟 下落加速度
        this.speed = 0;
        // 分数
        this.score = 0;
        this.total.string = this.score.toString();
        // 小鸟的位置 重置
        this.birdParent.y = this.birdParent.x = 0;
    }

    // 小鸟动起来
    bridyMovement(dt: number) {
        // 小鸟煽动翅膀
        this.time += dt;
        if (this.time > 0.3) {
            // 切换当前显示的 小鸟精灵
            if (this.bird0.node.active) {
                this.bird0.node.active = false;
                this.bird1.node.active = true;
            } else if (this.bird1.node.active) {
                this.bird1.node.active = false;
                this.bird2.node.active = true;
            } else if (this.bird2.node.active) {
                this.bird2.node.active = false;
                this.bird3.node.active = true;
            } else if (this.bird3.node.active) {
                this.bird3.node.active = false;
                this.bird0.node.active = true;
            }
            this.time = 0;
        }

        // 小鸟向下
        this.speed -= 0.1;
        this.birdParent.y += this.speed;
        this.birdParent.rotation = -this.speed * 10;
    }

    // 移动背景
    moveBG(bg: cc.Node) {
        bg.x -= 1;
        if (bg.x < -288) {
            bg.x += 288 * 2;
        }
    }

    // 初始化 管子
    initPipe() {
        let pipePraentStartOffsetx: number = 200;
        let sspaceX = (288 + 52) / 3;
        this.pipePraent0.x = pipePraentStartOffsetx + sspaceX * 0;
        this.pipePraent1.x = pipePraentStartOffsetx + sspaceX * 1;
        this.pipePraent2.x = pipePraentStartOffsetx + sspaceX * 2;
    }

    // 移动管子
    movePipe(pipe: cc.Node) {
        pipe.x -= 3;
        if (pipe.x < (-144 - 52)) {
            pipe.x += (288 + 52);
            pipe.y = 25 - (Math.random() * 50);
            this.score += 1;
            this.total.string = this.score.toString();
        }
    }


    /**
     *  手势 事件监听  暂时不行 后续再看
     */
    addEventHandler() {
        this.contentView.on('touchstart', (envent) => {
            cc.log('点击了')
            console.log('来呀 点位啊')
        });
    }

    // 点击屏幕 事件
    onButtonClick() {
        this.speed = 2.6;
    }

    // 碰撞检测
    checkCollision(bird: cc.Node, pipe: cc.Node) {
        if ((bird.x + 17 < pipe.x - 26) || (bird.x - 17 > pipe.x + 26) || ((bird.y + 12 < pipe.y + 50) && (bird.y - 12 > pipe.y - 50))) { return; }
        cc.log('碰撞 了')
        this.gameOverClick();
    }

    // 结束 事件
    gameOverClick() {
        this.isOver = true;
        this.gameOverView.active = true;
    }


}
